package com.zwj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zwj.bo.StudentBo;
import com.zwj.dto.PaginationDate;
import com.zwj.dto.Result;
import com.zwj.dto.ReturnDate;
import com.zwj.entity.StudentCourseEntity;
import com.zwj.entity.StudentEntity;
import com.zwj.mapper.StudentCourseMapper;
import com.zwj.mapper.StudentMapper;
import com.zwj.service.StudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zwj.utils.PaginationUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zwj
 * @since 2021-10-30
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, StudentEntity> implements StudentService {
    private final StudentMapper studentMapper;
    private final StudentCourseMapper studentCourseMapper;
    public StudentServiceImpl(StudentMapper studentMapper,StudentCourseMapper studentCourseMapper) {
        this.studentMapper = studentMapper;
        this.studentCourseMapper = studentCourseMapper;
    }
    @Override
    public Result addStudent(StudentEntity studentEntity) {
        //数据校验
       if(studentEntity.getStudName()==null||studentEntity.getStudName().equals("")){
           return Result.error().message("学生姓名不能为空");
       }
       if(studentEntity.getGrade()==null||studentEntity.getGrade()<1){
           return Result.error().message("年级不合法");
       }
        if(studentEntity.getSex()!=null&&(studentEntity.getSex()!=0 && studentEntity.getSex()!=1)){
            return Result.error().message("性别不和法");
        }
       //插入
       studentEntity.setId(null);
       studentEntity.setIsDeleted("0");
       int row = studentMapper.insert(studentEntity);
        //判断插入结果
       if (row == 0) {
           return Result.error().message("添加学生失败");
       }
       return Result.ok();
    }

    @Override
    public Result updateStudent(StudentEntity studentEntity) {
        //数据校验
        if(studentEntity.getId()==null||studentEntity.getId().equals("")){
            return Result.error().message("学生ID不能为空");
        }
        if(studentEntity.getStudName()==null||studentEntity.getStudName().equals("")){
            return Result.error().message("学生姓名不能为空");
        }
        if(studentEntity.getGrade()==null||studentEntity.getGrade()<1){
            return Result.error().message("年级不合法");
        }
        if(studentEntity.getSex()!=null&&(studentEntity.getSex()!=0 && studentEntity.getSex()!=1)){
            return Result.error().message("性别不和法");
        }
        studentEntity.setIsDeleted(null);
        UpdateWrapper<StudentEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(StudentEntity::getId, studentEntity.getId());
        int row = studentMapper.update(studentEntity, updateWrapper);
        //判断修改结果
        if (row == 0) {
            return Result.error().message("编辑学生失败");
        }
        return Result.ok();
    }
    @Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
    @Override
    public Result deleteStudent(StudentBo studentBo) {
        //数据校验
        if(studentBo.getId()==null||studentBo.getId().equals("")){
            return Result.error().message("学生ID不能为空");
        }
        QueryWrapper<StudentCourseEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(StudentCourseEntity::getStudentId,studentBo.getId());
        studentCourseMapper.delete(wrapper);
        StudentEntity studentEntity = new StudentEntity();
        studentEntity.setIsDeleted("1");
        UpdateWrapper<StudentEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(StudentEntity::getId, studentBo.getId()).eq(StudentEntity::getIsDeleted,'0');
        int row = studentMapper.update(studentEntity, updateWrapper);
        //判断修改结果
        if (row == 0) {
            return Result.error().message("删除学生失败");
        }
        return Result.ok();
    }

    @Override
    public Result seleteStudent(StudentBo studentBo) {
        //数据校验s
        if(!PaginationUtils.isPagingParameters(studentBo.getNumber(), studentBo.getPage())){
            return Result.error().message("分页参数错误!");
        }
        if(studentBo.getGrade()!=null&&studentBo.getGrade()<1){
            return Result.error().message("年级不合法");
        }
        if(studentBo.getSex()!=null&&(studentBo.getSex()!=0 && studentBo.getSex()!=1)){
            return Result.error().message("性别不和法");
        }
        int page = studentBo.getPage();
        studentBo.setPage(PaginationUtils.getPageIndex(studentBo.getPage(), studentBo.getNumber()));
        //查询项目组评分 mybatis
        List<Object> result = studentMapper.seleteStudent(studentBo);
        int sum = ((List<Integer>) result.get(1)).get(0);
        //记录总数为0
        if (sum == 0) return Result.error().message("查询结果为空");
        PaginationDate paginationDate = PaginationUtils.getPaginationDate(studentBo.getNumber(), page, sum);
        //当前页大于总页数
        if (PaginationUtils.isValid(paginationDate))
            return Result.error().message("分页参数错误!");
        List<StudentEntity> studentEntities =  (List<StudentEntity>) result.get(0);
        return Result.ok().data(new ReturnDate(paginationDate,studentEntities ));
    }
}
